﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;

namespace Framework.Common
{
    public static class StringExtension
    {
        /// <summary>
        /// 验证 String  非空
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ValidateNotNull(this string str)
        {
            var tmp = CommonValidator.isNotNull(str);
            if (tmp) return true;
            return false;
        }

        /// <summary>
        /// 验证 String  长度
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len">长度</param>
        /// <returns></returns>
        public static bool ValidateLen(this string str, int len)
        {
            if (!str.ValidateNotNull()) return false;

            if (str.Length <= len) return true;
            return false;
        }


        /// <summary>
        /// 验证 是否为整型
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ValidateInt(this string str)
        {
            if (!str.ValidateNotNull()) return false;
            var tmp = CommonValidator.isInt(str);
            if (tmp) return true;
            return false;
        }

        /// <summary>
        /// 验证 是否为金额
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ValidateMenoy(this string str)
        {
            if (!str.ValidateNotNull()) return false;
            if (str.StartsWith("-")) str = str.Substring(1);//负数
            var tmp = CommonValidator.isMoney(str);
            if (tmp) return true;
            return false;
        }

        /// <summary>
        /// 验证 集合非空
        /// id1,id2,id3
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ValidateArrayNotNull(this string str)
        {
            if (!str.ValidateNotNull()) return false;

            var tmp = str.Split(',').Count(a => !string.IsNullOrEmpty(a) && a != "[]");
            if (tmp > 0) return true;

            return false;
        }

        /// <summary>
        /// 验证 集合重复
        /// id1,id2,id3
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ValidateArrayNotRepeat(this string str)
        {
            if (!str.ValidateArrayNotNull()) return false;

            var tmp = str.Split(',').Where(a => !string.IsNullOrEmpty(a)).ToList();
            var length = tmp.Count;
            for (int i = 0; i < length - 1; i++)
            {
                for (int j = length - 1; j > i; j--)
                {
                    if (tmp[i] == tmp[j])
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 验证手机号码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ValidatePhone(this string str)
        {
            if (!str.ValidateNotNull()) return false;

            if (CommonValidator.isMobile(str)) return true;
            return false;
        }

        /// <summary>
        /// 验证身份证号码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ValidatePersonCode(this string str)
        {
            if (!str.ValidateNotNull()) return false;

            if (CommonValidator.isPersonCode(str)) return true;

            return false;
        }

    }
}
